home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / access.c next >
C/C++ Source or Header  |  1996-06-26  |  11KB  |  390 lines

  1. /* 
  2. This module is an adaption of code from the tcpd-1.4 package written
  3. by Wietse Venema, Eindhoven University of Technology, The Netherlands.
  4.  
  5. The code is used here with permission.
  6.  
  7. The code has been considerably changed from the original. Bug reports
  8. should be sent to Andrew.Tridgell@anu.edu.au
  9. */
  10.  
  11. #include "includes.h"
  12. #include "loadparm.h"
  13.  
  14. #define ALLOW_PURE_ADDRESSES
  15.  
  16. extern int DEBUGLEVEL;
  17.  
  18. #ifndef    INADDR_NONE
  19. #define    INADDR_NONE    ((unsigned long)~0)
  20. #endif
  21.  
  22.  
  23. #define FROM_ADDRLEN  (4*3+3+1)
  24. #define Good True
  25. #define Bad False
  26.  
  27. #define CLIENT_MATCH client_match
  28.  
  29. /* Delimiters for lists of daemons or clients. */
  30.  
  31. static char sep[] = ", \t";
  32.  
  33. /* Constants to be used in assignments only, not in comparisons... */
  34.  
  35. #define    YES        1
  36. #define    NO        0
  37. #define    FAIL        (-1)
  38.  
  39. /* Forward declarations. */
  40. BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client);
  41. static int list_match(char *list,char *item, int (*match_fn)());
  42. static int client_match(char *tok,char *item);
  43. static int string_match(char *tok,char *s);
  44. static int masked_match(char *tok, char *slash, char *s);
  45. static int matchname(char *remotehost,struct in_addr  addr);
  46. BOOL fromhost(int sock,struct from_host *f);
  47.  
  48.  
  49. /* Size of logical line buffer. */
  50. #define    BUFLEN 2048
  51.  
  52.  
  53. /* return true if access should be allowed to a service*/
  54. BOOL check_access(int snum)
  55. {
  56.   extern int Client;
  57.   extern struct from_host Client_info;
  58.   char *denyl,*allowl;
  59.   BOOL ret = False;
  60.  
  61.   denyl = lp_hostsdeny(snum);
  62.   if (denyl) denyl = strdup(denyl);
  63.  
  64.   allowl = lp_hostsallow(snum);
  65.   if (allowl) allowl = strdup(allowl);
  66.  
  67.  
  68.   fromhost(Client,&Client_info);
  69.  
  70.   if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
  71.     ret = True;
  72.  
  73.   if (!ret)
  74.     {
  75.       if (!fromhost(Client,&Client_info))
  76.     DEBUG(0,("ERROR: Can't get from_host info\n"));
  77.       else
  78.     {
  79.       if (allow_access(denyl,allowl,&Client_info))
  80.         {
  81.           if (snum >= 0)
  82.         DEBUG(2,("Allowed connection from %s (%s) to %s\n",
  83.              Client_info.name,Client_info.addr,
  84.              lp_servicename(snum)));
  85.           ret = True;
  86.         }
  87.       else
  88.         if (snum >= 0)
  89.           DEBUG(0,("Denied connection from %s (%s) to %s\n",
  90.                Client_info.name,Client_info.addr,
  91.                lp_servicename(snum)));
  92.     }
  93.     }
  94.  
  95.   if (denyl) free(denyl);
  96.   if (allowl) free(allowl);
  97.   return(ret);
  98. }
  99.  
  100.  
  101. /* return true if access should be allowed */
  102. BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client)
  103. {
  104.   /* if theres no deny list and no allow list then allow access */
  105.   if ((!deny_list || *deny_list == 0) && (!allow_list || *allow_list == 0))
  106.     return(True);  
  107.  
  108.   /* if there is an allow list but no deny list then allow only hosts
  109.      on the allow list */
  110.   if (!deny_list || *deny_list == 0)
  111.     return(list_match(allow_list,(char *)client,CLIENT_MATCH));
  112.  
  113.   /* if theres a deny list but no allow list then allow
  114.      all hosts not on the deny list */
  115.   if (!allow_list || *allow_list == 0)
  116.     return(!list_match(deny_list,(char *)client,CLIENT_MATCH));
  117.  
  118.   /* if there are both type of list then allow all hosts on the allow list */
  119.   if (list_match(allow_list,(char *)client,CLIENT_MATCH))
  120.     return (True);
  121.  
  122.   /* if there are both type of list and it's not on the allow then
  123.      allow it if its not on the deny */
  124.   if (list_match(deny_list,(char *)client,CLIENT_MATCH))
  125.     return (False);
  126.  
  127.   return (True);
  128. }
  129.  
  130. /* list_match - match an item against a list of tokens with exceptions */
  131. /* (All modifications are marked with the initials "jkf") */
  132. static int list_match(char *list,char *item, int (*match_fn)())
  133. {
  134.     char   *tok;
  135.     char   *listcopy;        /* jkf */
  136.     int     match = NO;
  137.  
  138.     /*
  139.      * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
  140.      * overwriting the list given as its first parameter.
  141.      */
  142.  
  143.     /* jkf -- can get called recursively with NULL list */
  144.     listcopy = (list == 0) ? (char *)0 : strdup(list);
  145.  
  146.     /*
  147.      * Process tokens one at a time. We have exhausted all possible matches
  148.      * when we reach an "EXCEPT" token or the end of the list. If we do find
  149.      * a match, look for an "EXCEPT" list and recurse to determine whether
  150.      * the match is affected by any exceptions.
  151.      */
  152.  
  153.     for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
  154.     if (strcasecmp(tok, "EXCEPT") == 0)    /* EXCEPT: give up */
  155.         break;
  156.     if ((match = (*match_fn) (tok, item)))    /* YES or FAIL */
  157.         break;
  158.     }
  159.     /* Process exceptions to YES or FAIL matches. */
  160.  
  161.     if (match != NO) {
  162.     while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
  163.          /* VOID */ ;
  164.     if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
  165.         if (listcopy != 0) free(listcopy); /* jkf */
  166.         return (match);
  167.     }
  168.     }
  169.  
  170.     if (listcopy != 0) free(listcopy); /* jkf */
  171.     return (NO);
  172. }
  173.  
  174.  
  175. /* client_match - match host name and address against token */
  176. static int client_match(char *tok,char *item)
  177. {
  178.     struct from_host *client = (struct from_host *) item;
  179.     int     match;
  180.  
  181.     /*
  182.      * Try to match the address first. If that fails, try to match the host
  183.      * name if available.
  184.      */
  185.  
  186.     if ((match = string_match(tok, client->addr)) == 0)
  187.     if (client->name[0] != 0)
  188.         match = string_match(tok, client->name);
  189.     return (match);
  190. }
  191.  
  192. /* string_match - match string against token */
  193. static int string_match(char *tok,char *s)
  194. {
  195.     int     tok_len;
  196.     int     str_len;
  197.     char   *cut;
  198.  
  199.     /*
  200.      * Return YES if a token has the magic value "ALL". Return FAIL if the
  201.      * token is "FAIL". If the token starts with a "." (domain name), return
  202.      * YES if it matches the last fields of the string. If the token has the
  203.      * magic value "LOCAL", return YES if the string does not contain a "."
  204.      * character. If the token ends on a "." (network number), return YES if
  205.      * it matches the first fields of the string. If the token begins with a
  206.      * "@" (netgroup name), return YES if the string is a (host) member of
  207.      * the netgroup. Return YES if the token fully matches the string. If the
  208.      * token is a netnumber/netmask pair, return YES if the address is a
  209.      * member of the specified subnet.
  210.      */
  211.  
  212.     if (tok[0] == '.') {            /* domain: match last fields */
  213.     if ((str_len = strlen(s)) > (tok_len = strlen(tok))
  214.         && strcasecmp(tok, s + str_len - tok_len) == 0)
  215.         return (YES);
  216.     } else if (tok[0] == '@') {            /* netgroup: look it up */
  217. #ifdef    NETGROUP
  218.       static char *mydomain = NULL;
  219.       char *hostname = NULL;
  220.       BOOL netgroup_ok = False;
  221.  
  222.       if (!mydomain) yp_get_default_domain(&mydomain);
  223.  
  224.       if (!(hostname = strdup(s))) {
  225.     DEBUG(1,("out of memory for strdup!\n"));
  226.     return NO;
  227.       }
  228.  
  229.       netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
  230.  
  231.       DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n", 
  232.            hostname,
  233.            mydomain, 
  234.            tok+1,
  235.            BOOLSTR(netgroup_ok)));
  236.  
  237. #ifdef NETGROUP_INSECURE
  238.       /* if you really want netgroups that match non qualified names
  239.      then define NETGROUP_INSECURE. It can, however, be a big
  240.      security hole */
  241.       {
  242.     char        *clnt_domain;
  243.     if (!netgroup_ok && (clnt_domain=strchr(hostname,'.'))) {
  244.       *clnt_domain++ = '\0';
  245.       netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
  246.     }
  247.       }
  248. #endif
  249.  
  250.       free(hostname);
  251.       
  252.       if (netgroup_ok) return(YES);
  253. #else
  254.       DEBUG(0,("access: netgroup support is not configured"));
  255.       return (NO);
  256. #endif
  257.     } else if (strcasecmp(tok, "ALL") == 0) {    /* all: match any */
  258.     return (YES);
  259.     } else if (strcasecmp(tok, "FAIL") == 0) {    /* fail: match any */
  260.     return (FAIL);
  261.     } else if (strcasecmp(tok, "LOCAL") == 0) {    /* local: no dots */
  262.     if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
  263.         return (YES);
  264.     } else if (!strcasecmp(tok, s)) {    /* match host name or address */
  265.     return (YES);
  266.     } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {    /* network */
  267.     if (strncmp(tok, s, tok_len) == 0)
  268.         return (YES);
  269.     } else if ((cut = strchr(tok, '/')) != 0) {    /* netnumber/netmask */
  270.     if (isdigit(s[0]) && masked_match(tok, cut, s))
  271.         return (YES);
  272.     }
  273.     return (NO);
  274. }
  275.  
  276. /* masked_match - match address against netnumber/netmask */
  277. static int masked_match(char *tok, char *slash, char *s)
  278. {
  279.     unsigned long net;
  280.     unsigned long mask;
  281.     unsigned long addr;
  282.  
  283.     if ((addr = interpret_addr(s)) == INADDR_NONE)
  284.     return (NO);
  285.     *slash = 0;
  286.     net = interpret_addr(tok);
  287.     *slash = '/';
  288.     if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
  289.     DEBUG(0,("access: bad net/mask access control: %s", tok));
  290.     return (NO);
  291.     }
  292.     return ((addr & mask) == net);
  293. }
  294.  
  295.  
  296. /* fromhost - find out what is at the other end of a socket */
  297. BOOL fromhost(int sock,struct from_host *f)
  298. {
  299.     static struct sockaddr sa;
  300.     struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
  301.     struct hostent *hp;
  302.     int     length = sizeof(sa);
  303.     static char addr_buf[FROM_ADDRLEN];
  304.     static char name_buf[MAXHOSTNAMELEN];
  305.     BOOL   takeAddressAsHostname = False;
  306.  
  307.     if (getpeername(sock, &sa, &length) < 0) 
  308.       {
  309.     DEBUG(0,("getpeername failed\n"));
  310.     return(False);
  311.       }
  312.  
  313.     f->sin = sockin;
  314.     f->addr = strcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
  315.  
  316.     /* Look up the remote host name. */
  317.     if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
  318.                 sizeof(sockin->sin_addr),
  319.                 AF_INET)) == 0) {
  320.       DEBUG(0,("Gethostbyaddr failed\n"));
  321. #ifdef ALLOW_PURE_ADDRESSES
  322.       takeAddressAsHostname = True;
  323. #else
  324.       return(False);
  325. #endif
  326.     }
  327.  
  328.     /* Save the host name. A later gethostbyxxx() call may clobber it. */
  329.     f->name = StrnCpy(name_buf,
  330.                       takeAddressAsHostname? f->addr : hp->h_name,
  331.                       sizeof(name_buf) - 1);
  332.  
  333.     /*
  334.      * Verify that the host name does not belong to someone else. If host
  335.      * name verification fails, pretend that the host name lookup failed.
  336.      */
  337.     if (!takeAddressAsHostname && !matchname(f->name, sockin->sin_addr))
  338.       {
  339.     DEBUG(0,("Matchname failed\n"));
  340.     return(False);
  341.       }
  342.  
  343.     return(True);    
  344. }
  345.  
  346. /* matchname - determine if host name matches IP address */
  347. static int matchname(char *remotehost,struct in_addr  addr)
  348. {
  349.   struct hostent *hp;
  350.   int     i;
  351.   
  352.   if ((hp = Get_Hostbyname(remotehost)) == 0) {
  353.     DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
  354.     return (Bad);
  355.   } 
  356.  
  357.     /*
  358.      * Make sure that gethostbyname() returns the "correct" host name.
  359.      * Unfortunately, gethostbyname("localhost") sometimes yields
  360.      * "localhost.domain". Since the latter host name comes from the
  361.      * local DNS, we just have to trust it (all bets are off if the local
  362.      * DNS is perverted). We always check the address list, though.
  363.      */
  364.   
  365.   if (strcasecmp(remotehost, hp->h_name)
  366.       && strcasecmp(remotehost, "localhost")) {
  367.     DEBUG(0,("host name/name mismatch: %s != %s",
  368.       remotehost, hp->h_name));
  369.     return (Bad);
  370.   }
  371.     
  372.   /* Look up the host address in the address list we just got. */
  373.   for (i = 0; hp->h_addr_list[i]; i++) {
  374.     if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
  375.       return (Good);
  376.   }
  377.  
  378.   /*
  379.    * The host name does not map to the original host address. Perhaps
  380.    * someone has compromised a name server. More likely someone botched
  381.    * it, but that could be dangerous, too.
  382.    */
  383.   
  384.   DEBUG(0,("host name/address mismatch: %s != %s",
  385.     inet_ntoa(addr), hp->h_name));
  386.   return (Bad);
  387. }
  388.  
  389.  
  390.